package com.px.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.px.dal.ProjectClassMapper;
import com.px.model.ProjectClass;
import com.px.model.getData;
import com.px.unit.Response;

@Controller
public class ProjectClassController {
	// 实例化我们的数据操作类
		ProjectClassMapper projectClass_mapper = new ProjectClassMapper();

		/**
		 * 获取所有项目分类信息
		 * 
		 */
		@ResponseBody
		@CrossOrigin
		@RequestMapping(value = "/getAllProjectClass", method = RequestMethod.GET, produces = "application/json; utf-8")
		public Response getAllUsers() {
			// 示例化一个自定义对象，用来传递我们需要的参数
			getData mydata = new getData();
			List<ProjectClass> projectClassList = projectClass_mapper.getAllProjectClass();
			mydata.setProjectClassList(projectClassList);
			return new Response(200, "获取成功！", mydata);
		}
		/**
		 * 获取指定Id项目信息
		 * 
		 */
		@ResponseBody
		@CrossOrigin
		@RequestMapping(value = "/getProjectClassByProjectClassId", method = RequestMethod.POST, produces = "application/json; utf-8")
		public Response getProjectClassByProjectClassId(@RequestBody ProjectClass projectClass) {
			if (projectClass != null) {
				// 判断查询的项目分类是否存在
				int checkid = projectClass_mapper.checkProjectClassIdIsExist(projectClass);
				if (!(checkid > 0)) {
					return new Response(304, "该项目分类不存在");
				} else {
					// 示例化一个自定义对象，用来传递我们需要的参数
					getData mydata = new getData();
					List<ProjectClass> projectClassList = projectClass_mapper.getProjectClassByProjectClassId(projectClass);
					mydata.setProjectClassList(projectClassList);
					return new Response(200, "获取成功！", mydata);
				}
			}
			return new Response(304, "获取失败！");

		}
		/**
		 * 添加项目分类信息
		 * 
		 */
		@ResponseBody
		@CrossOrigin
		@RequestMapping(value = "/insertProjectClass", method = RequestMethod.POST, produces = "application/json; utf-8")
		public Response insertProjectClass(@RequestBody ProjectClass projectClass) {

			if (projectClass != null) {

				// 判断添加的项目分类账户不能重复
				int checkname = projectClass_mapper.checkProjectClassIdIsExist(projectClass);
				if (checkname > 0) {
					return new Response(304, "已存在同名帐号");
				} else {
					int counts = projectClass_mapper.insertProjectClass(projectClass);
					if (counts > 0) {
						return new Response(200, "添加成功！");
					} else {
						return new Response(304, "添加失败！");
					}
				}
			}
			return new Response(304, "添加失败！");
		}

		/**
		 * 根据项目分类ID删除项目分类信息
		 * 
		 */
		@ResponseBody
		@CrossOrigin
		@RequestMapping(value = "/deleteProjectClassByProjectClassId", method = RequestMethod.DELETE, produces = "application/json; utf-8")
		public Response deleteProjectClassByProjectClassId(@RequestBody ProjectClass projectClass) {

			if (projectClass != null) {

				// 判断删除的项目分类是否存在
				int checkid = projectClass_mapper.checkProjectClassIdIsExist(projectClass);
				if (!(checkid > 0)) {
					return new Response(304, "该项目分类不存在");
				} else {
					int counts = projectClass_mapper.deleteProjectClassByProjectClassId(projectClass);
					if (counts > 0) {
						return new Response(200, "删除成功！");
					} else {
						return new Response(304, "删除失败！");
					}
				}
			}
			return new Response(304, "删除失败！");
		}

		/**
		 * 根据会员单位负责id修改会员项目分类信息
		 * 
		 */
		@ResponseBody
		@CrossOrigin
		@RequestMapping(value = "/updateProjectClassByProjectClassId", method = RequestMethod.PUT, produces = "application/json; utf-8")
		public Response updateProjectClassByProjectClassId(@RequestBody ProjectClass projectClass) {

			if (projectClass != null) {

				// 判断修改的会员单位负责人是否存在
				int checkname = projectClass_mapper.checkProjectClassIdIsExist(projectClass);
				if (!(checkname > 0)) {
					return new Response(304, "该项目分类不存在");
				} else {
					int counts = projectClass_mapper.updateProjectClassByProjectClassId(projectClass);
					if (counts > 0) {
						return new Response(200, "修改成功！");
					} else {
						return new Response(304, "修改失败！");
					}
				}
			}
			return new Response(304, "修改失败！");
		}
		
}
